
import echarts from 'echarts';
// 引入柱状图
import 'echarts/lib/chart/bar';
// 引入提示框和标题组件
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';

function safeAcivityCtrl($log, $http, $scope, $timeout, cfpLoadingBar) {
  'ngInject';
  let _this = this;
  this.paginationData = {
    pageNum: 1,
    pageSize: 6,
    total: '6',
  };

  this.getCheckStatus = (status) => {
    switch(status) {
      case 0:
        return '未开始';
        break;
      case 1:
        return '进行中';
        break;
      case 2:
        return '待整改';
        break;
      case 3:
        return '已完成';
        break;
    }
  }

  // 近期检查情况总览echarts
  this.chartNearInit = () => {
    var myChart = echarts.init(document.getElementById('recentCheckTotal'));
    var option = {
      color: ['#fbae9f','#6eb5e5'],
      tooltip: {
        trigger: 'axis'
      },
      grid: {
        top:'2%',
        left: '3%',
        right: '8%',
        bottom: '8%',
        containLabel: true
      },
      calculable : true,
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: _this.nearList.checkClassifyName
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          name:'日常巡查',
          type:'line',
          data:_this.nearList.dailyCheckTimes
        },
        {
          name:'专项检查',
          type:'line',
          data:_this.nearList.specialCheckTimes
        }
      ]
    };
    myChart.setOption(option);
  }

  // 重点项目巡查次数echarts
  this.chartsMainInit = (data) => {
    var option = {
      tooltip : {
        trigger: 'item',
        formatter: "{a} <br/>{b} : {c} ({d}%)"
      },
      title : {
        text: '巡查次数',
        left:'44%',
        y:'center',
        textStyle: {color: '#666'}
      },
      color: ['#fe6f4d', '#fcc638', '#2de88e', '#a8a8a8', '#299be4'],
      legend: {
        orient: 'vertical',
        y:10,
        right:0,
        data: _this.nearList.checkClassifyName
      },
      series : [
        {
          name: '次数占比',
          type: 'pie',
          radius: ['45%', '65%'],
          center: ['50%', '50%'],
          data: data,
          itemStyle: {
            emphasis: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    };
    let myChart = echarts.init(document.getElementById('inspect-key-projects'));
    myChart.setOption(option);

  }

  var nearDate = new Date(); 

  // 获取近期检查情况总览
  this.getNearFuture = () => {
    let reqData = {
      queryDate:nearDate
    }
    $http.post(`${SERVER_CONTEXT}/api/securityActivities/specialCheck/queryCheckSituationOverviewByTime`,
      reqData
        )
        .then((resp) => {            
          this.nearQueryList = resp.data;
          let list = {},special = {},daily = {};
          list.checkClassifyName = [];
          list.checkTimes = [];
          special.checkTimes = []; // 专项检查次数
          daily.checkTimes = []; // 日常巡查检查次数

          // 获取echarts的data
          let dataArr = [];
          
          // 所有
          this.nearQueryList.all.forEach((item) => {
            list.checkClassifyName.push(item.checkClassifyName);
            list.checkTimes.push(item.checkTimes);
            dataArr.push({
              name:item.checkClassifyName,
              value:item.checkTimes
            })
          });
          // 专项
          this.nearQueryList.special.forEach((item) => {
            special.checkTimes.push(item.checkTimes);            
          });
          // 日常
          this.nearQueryList.daily.forEach((item) => {
            daily.checkTimes.push(item.checkTimes);
          });  
          
          this.nearList = {
            checkClassifyName: list.checkClassifyName,
            specialCheckTimes: special.checkTimes,
            dailyCheckTimes: daily.checkTimes,
            checkTimes: list.checkTimes
          };
          
          this.chartNearInit (); // 近期检查情况总览echarts 
          this.chartsMainInit(dataArr); // 重点项目巡查次数echarts          
        })
  }

  // 获取安全问题汇总
  this.getSafeList = () => {
    $http.post(`${SERVER_CONTEXT}/api/securityActivities/checkProblem/queryCheckProblemListByCondition`,
      this.paginationData
        ).
      then((resp) => {
        this.meetingList = resp.data.data;
      });
  }

  // 获取专项检查任务
  this.getSpecialCheck = () => {
    $http.post(`${SERVER_CONTEXT}/api/securityActivities/specialCheck/querySpecialCheckByCondition`,
          this.paginationData
            ).
          then((resp) => {
            this.securityQuestionTotal = resp.data.data;
          });
  }

  this.initPage = () => {
    // 获取近期检查情况总览
    this.getNearFuture();

    // 获取安全问题汇总
    this.getSafeList();

    // 获取专项检查任务
    this.getSpecialCheck();
  }

  // 初始化页面
  this.initPage();

}

export default safeAcivityCtrl;